﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Detai.Site.Models;

namespace Detai.Site.Components
{
    public class BaseAuthentication
    {

        //protected string CurrentUserName
        //{
        //    get
        //    {
        //        User user = CurrentUser;
        //        return user == null ? string.Empty : user.UserName;
        //    }
        //}
        protected string CurrentUserName
        {
            get
            {
                IFormsAuthenticationService iform = new FormsAuthenticationService();
                string strUserName = iform.GetCurrentUserName();
                return strUserName;
            }
        }

        private int? CurentUserId
        {
            get
            {
                IFormsAuthenticationService iform = new FormsAuthenticationService();
                string strUserId = iform.GetCurrentUserName();
                return Convert.ToInt32(strUserId);
            }
        }

        public TaiKhoan CurrentUser
        {
            get
            {
                string userName = CurrentUserName;

                if (!string.IsNullOrEmpty(userName))
                {
                    DetaiDataContext db = new DetaiDataContext();
                    var currentUser = db.TaiKhoans.FirstOrDefault(e => e.Tendangnhap == userName);
                    return currentUser;
                }
                return null;
            }
        }

        public void Login(string userName)
        {
            IFormsAuthenticationService iform = new FormsAuthenticationService();
            iform.SignIn(userName);
        }

        public void Logout()
        {
            IFormsAuthenticationService iform = new FormsAuthenticationService();
            iform.SignOut();
        }
    }
}
